<?php
class Controller_WWW_Comments extends Controller_WWW_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:www/comments/index";
        parent::__construct($req, $res);
    }

    public function action_index ()
    {
        $this->action_create();
    }

    public function action_create()
    {
        $post = $this->request->post();

//        $action = Arr::get($post,'action');
        $uid = Arr::get($post,'uid');
        $tid = Arr::get($post,'tid');
        $comments = Arr::get($post,'comments');

        //$data['table'] = 'mod_scenic_spot';

        $data['uid'] = $uid;
        $data['targetId'] = $tid;
        $data['comments'] = $comments;
        $data['created'] = time();
        $obj = new Comments();
        if($obj->insert($data) > 0){
            echo json_encode(array('status'=>0,'msg'=>'成功'));
            exit;
        };
        echo json_encode(array('status'=>1,'msg'=>'失败'));
//        $this->request->redirect('/scenicspot/detail/'.$tid);
        exit;
    }

    public function action_get_comments(){
        $query = $this->request->query();
        $action = 'scenicspot';
//        $action = Arr::get($query,'action');
        $tid = Arr::get($query,'tid');

        $obj = new Comments();
        $data = $obj->getComments($action,$tid);
        $this->template->set('data',$data);
//        var_dump($data);
        if ($this->request->is_ajax())
        {
            $this->template->set_filename('smarty:www/comments/item');
            $rs = $this->template->render();
            echo $rs;
//            var_dump($rs);
            exit;
        }

    }

    public function action_more_comments($page = 2){
        $query = $this->request->query();
        $action = 'scenicspot';
        $tid = Arr::get($query,'tid');
        $page   = max($page, 1);
        $limit  = 5;
        $offset = ($page - 1) * $limit;
        $obj = new Comments();
        $data = $obj->getComments($action,$tid,$limit,$offset);

        $this->template->set('data',$data);
        $this->template->set_filename('smarty:www/comments/item');
        $rs = $this->template->render();
        echo $rs;
        exit;
    }

}
